package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.common.vo.Tree;
import com.aisile.crowd_funding.system.pojo.SysMenu;
import com.aisile.crowd_funding.system.pojo.SysMenuExample;
import com.aisile.crowd_funding.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/sys/menu")
public class MenuController {
    @Autowired
    MenuService menuService;

    public  int paernt;

    @RequestMapping
    public String menu(){

        return "System/menu/menu";
    }
    //查询菜单列表
    @RequestMapping("/list")
    @ResponseBody
    public List<SysMenu> getMenuList(){

        return menuService.selectByExample(null);
    }

    @RequestMapping("/add")
    public String toadd(@RequestParam(value = "parentId",defaultValue = "0") Long parentId, Model model){
       if(parentId==0){
           model.addAttribute("pName","根目录");
       }else{
           SysMenu sysMenu = menuService.selectByPrimaryKey(parentId);
           model.addAttribute("pName",sysMenu.getName());
           paernt=sysMenu.getType();
       }
       model.addAttribute("parentId",parentId);
       return "System/menu/add";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public R save(SysMenu sysMenu){
        int insert=menuService.insert(sysMenu);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }


    @RequestMapping("/hh")
    @ResponseBody
    public int hh(){
        int s=this.paernt;
        return s;

    }

    @RequestMapping("/del")
    @ResponseBody
    public R del(Long menuId){
       int i=menuService.selectCount(menuId);
       if(i>0){
               return  R.error("请先删除子节点");
       }else{
           int p=menuService.deleteByPrimaryKey(menuId);
           if(p>0){
               return R.ok();
           }
       }
        return R.ok();

    }

    @RequestMapping("/edit")
    public String getSysMenu(Long menuId,Model model){
           SysMenu sysMenu=menuService.selectByPrimaryKey(menuId);
           //如果他的父节点就是0那么它本身就是根目录
           if(sysMenu.getParentId()==0){
               model.addAttribute("pName","根目录");
           }else{
               //这是查询他的父节点
               SysMenu sys = menuService.selectByPrimaryKey(sysMenu.getParentId());
               model.addAttribute("pName",sys.getName());
           }
           //把它本身传到修改页面用于回显
          model.addAttribute("menu",sysMenu);
        return "System/menu/update";
    }


    @RequestMapping("update")
    @ResponseBody
    public R update(SysMenu sysMenu){
        int i=menuService.updateByPrimaryKey(sysMenu);
        if(i>0){
            return R.ok();
        }else{
            return R.error("修改失败");
        }
    }
    @RequestMapping(value = "tree")
    @ResponseBody
    public Tree getTree(){

        return menuService.getTree();
    }

    @RequestMapping("tree/{roleId}")
    @ResponseBody
    public Tree getEditTree(@PathVariable Long roleId){

        return menuService.getEditTree(roleId);

    }
}
